﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RareEdge
{
	public partial class frmSettings : Form
	{
		GMobileSync gMobileSync = GMobileSync.Load();

		public frmSettings()
		{
			InitializeComponent();
		}

		private void mnuCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private bool ValidateForm()
		{
			if ((cboSyncPreviousDays.Text == ""))
			{
				MessageBox.Show("Please select the number of previous days to sync");
				cboSyncPreviousDays.Focus();
				return false;
			}
			if ((cboSyncFutureDays.Text == ""))
			{
				MessageBox.Show("Please select the number of future days to sync");
				cboSyncFutureDays.Focus();
				return false;
			}
			if (cbUseProxy.Checked)
			{
				if (txtProxyServer.Text.Trim().Length == 0)
				{
					MessageBox.Show("Please enter your proxy address");
					txtProxyServer.Focus();
					return false;
				}
				try
				{
					int.Parse(txtProxyPort.Text.Trim());
				}
				catch
				{
					MessageBox.Show("Please enter your proxy port. It should be a number");
					txtProxyPort.Focus();
					return false;
				}
			}
			return true;
		}

		private void mnuSave_Click(object sender, EventArgs e)
		{
			try
			{
				if (ValidateForm())
				{
					gMobileSync.Timezone = cboTimezone.Text;
					gMobileSync.SyncPreviousDays = Convert.ToInt32(cboSyncPreviousDays.Text);
					gMobileSync.SyncFutureDays = Convert.ToInt32(cboSyncFutureDays.Text);
					gMobileSync.ExitOnSyncComplete = chkExitOnComplete.Checked;
                    gMobileSync.IsByassNetworkTest = chkByassNetworkTest.Checked;

                    gMobileSync.UseProxy = cbUseProxy.Checked;
					if (cbUseProxy.Checked)
					{
						gMobileSync.ProxyServer = txtProxyServer.Text.Trim();
						gMobileSync.ProxyPort = int.Parse(txtProxyPort.Text.Trim());
					}
					gMobileSync.Save();

					this.Close();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void frmSettings_Load(object sender, EventArgs e)
		{
			// Generate timezone list
			string[] timezones = RareEdge.Properties.Resources.Timezones.Split(',');

			foreach (string timezone in timezones)
				cboTimezone.Items.Add(timezone);

			// Genreate past options
			for (int prev = 0; prev <= 60; prev++)
				cboSyncPreviousDays.Items.Add(prev.ToString());

			// Genreate future possibilities!
			for (int future = 0; future <= 365; future++)
				cboSyncFutureDays.Items.Add(future.ToString());

			// Load previous settings
			cboTimezone.Text = gMobileSync.Timezone;
			cboSyncPreviousDays.Text = gMobileSync.SyncPreviousDays.ToString();
			cboSyncFutureDays.Text = gMobileSync.SyncFutureDays.ToString();
			chkExitOnComplete.Checked = gMobileSync.ExitOnSyncComplete;
            chkByassNetworkTest.Checked = gMobileSync.IsByassNetworkTest;
 
            // Load Proxy settings
            cbUseProxy.Checked = gMobileSync.UseProxy;
            txtProxyServer.Text = gMobileSync.ProxyServer.ToString();
            txtProxyPort.Text = gMobileSync.ProxyPort.ToString();
		}
	}
}